package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.dto.FlightRefundOrderDto;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.sc.SearchFlightOrderCriteria;
import cn.buk.qms.service.FlightRefundOrderService;
import cn.buk.tms.flight.dto.response.RefundOrderResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 机票退票控制器
 * @author yfdai
 */
@RestController
@RequestMapping("/order/flight/refund")
public class OrderRefundController extends BaseController {

  @Autowired
  public OrderRefundController(FlightRefundOrderService service) {
    this.service = service;
  }

  private final FlightRefundOrderService service;

  /**
   * 创建机票退票订单
   */
  @PostMapping(value = "/create", consumes = "application/json")
  public JsonResult createFlightRefundOrder(HttpServletRequest request,
      @RequestBody FlightRefundOrderDto dto) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return service.createFlightRefundOrder(enterpriseId, operator, dto);
  }

  /**
   * 创建机票退票订单(多人一起提交）
   */
  @PostMapping(value = "/createV2", consumes = "application/json")
  public JsonResult createFlightRefundOrderV2(HttpServletRequest request,
      @RequestBody FlightRefundOrderDto dto) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return service.createFlightRefundOrderV2(enterpriseId, operator, dto);
  }

  /**
   * 查找退票订单
   */
  @RequestMapping("/")
  public CommonDto<RefundOrderResponseDto> searchFlightRefundOrders(HttpServletRequest request,
      @ModelAttribute("sc") SearchFlightOrderCriteria sc) {
    final int enterpriseId = getEnterpriseId(request);
    final int userId = getUserId(request);

    return service.searchFlightRefundOrder2(enterpriseId, userId, sc);
  }

  /**
   * “我来处理”处理退票
   */
  @RequestMapping("/{id}/status/processed")
  public JsonResult processFlightRefund(HttpServletRequest request,
      @PathVariable("id") int orderId) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return service.processFlightRefund(enterpriseId, operator, orderId);
  }

  /**
   * 取消退票订单
   */
  @RequestMapping("/{id}/status/canceled")
  public JsonResult cancelFlightRefund(HttpServletRequest request,
      @PathVariable("id") int refundTicketId) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return service.cancelFlightRefund(enterpriseId, operator, refundTicketId);
  }

  /**
   * 重新打开已取消的退票单
   */
  @RequestMapping("/{id}/status/reopened")
  public JsonResult reopenFlightRefund(HttpServletRequest request,
      @PathVariable("id") int refundTicketId) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return service.reopenFlightRefund(enterpriseId, operator, refundTicketId);
  }

  /**
   * 已提交退票
   */
  @PostMapping("/{id}/status/submitted")
  public JsonResult submitFlightRefund(HttpServletRequest request,
      @PathVariable("id") int refundTicketId) {
    return service.submitFlightRefund(getEnterpriseId(request), getUsername(request), refundTicketId);
  }

  /**
   * 航司预退
   */
  @RequestMapping("/{id}/status/airlineRefunded")
  public JsonResult setFlightRefundAirlineRefunded(HttpServletRequest request,
      @PathVariable("id") int id) {
    return service.setFlightRefundAirlineRefundedStatus(getEnterpriseId(request), getUsername(request), id);
  }

  /**
   * 航司实退
   */
  @RequestMapping("/{id}/status/finallyAirRefunded")
  public JsonResult setFinallyAirRefunded(HttpServletRequest request,
      @PathVariable("id") int id,
      @RequestParam("amount") double amount) {
    return service.setFinallyAirRefunded(getEnterpriseId(request), getUsername(request), id, amount);
  }

  /**
   * 预计航司退款金额(航司预退金额)
   */
  @RequestMapping("/{id}/airlineRefundAmount")
  public JsonResult setFlightRefundAirlineRefundAmount(HttpServletRequest request,
      @PathVariable("id") int refundTicketId,
      @RequestParam("amount") double amount) {
    return service.setFlightRefundAirlineRefundedAmount(getEnterpriseId(request), getUsername(request), refundTicketId, amount);
  }

  /**
   * 已退客户
   */
  @RequestMapping("/{id}/status/passengerRefunded")
  public JsonResult setFlightRefundPassengerRefundedStatus(HttpServletRequest request,
      @PathVariable("id") int id) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return service.setFlightRefundPassengerRefundedStatus(enterpriseId, username, id);
  }

  /**
   * 实退客户金额
   */
  @RequestMapping("/{id}/passengerRefundedAmount")
  public JsonResult setFlightRefundPassengerRefundedAmount(HttpServletRequest request,
      @PathVariable("id") int id,
      @RequestParam("amount") double amount)  {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return service.setFlightRefundPassengerRefundedAmount(enterpriseId, username, id, amount);
  }

  /**
   * 测试：发送退票单的模板信息
   */
  @RequestMapping("/{id}/weixinTplMsg")
  public JsonResult sendWeixinTplMsg(HttpServletRequest request,
                                                           @PathVariable("id") int id)  {
    final int enterpriseId = getEnterpriseId(request);
    return service.sendWeixinTplMsg(enterpriseId, id);
  }


  /**
   * 获取退票订单详情
   */
  @RequestMapping("/{id}")
  public RefundOrderResponseDto getFlightRefundOrderDetail(HttpServletRequest request,
      @PathVariable("id") int id
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final int userId = getUserId(request);

    return service.getFlightRefundOrderDetail(enterpriseId, userId, id);
  }

  /**
   * 获取退票订单详情
   */
  @RequestMapping("/refundOrderNo/{refundOrderNo}")
  public RefundOrderResponseDto getFlightRefundOrderDetailByRefundOrderNo(HttpServletRequest request,
      @PathVariable("refundOrderNo") String refundOrderNo
  ) {
    final int enterpriseId = getEnterpriseId(request);
    return service.getFlightRefundOrderDetailByRefundOrderNo(enterpriseId, refundOrderNo);
  }

  /**
   * 获取退票订单操作记录
   */
  @RequestMapping("/{id}/history")
  public List<OperationLog> searchFlightRefundOrderHistory(HttpServletRequest request,
      @PathVariable("id") int id
  ) {
    final int enterpriseId = getEnterpriseId(request);

    return service.searchFlightRefundOrderHistory(enterpriseId, id);
  }

  /**
   * 修改退票单的价格（航司退票费，我司服务费
   */
  @PostMapping(value = "/{id}/price")
  public JsonResult updateRefundOrderPrice(HttpServletRequest request,
      @PathVariable("id") int id,
      @RequestParam("airRefundCharge") double airRefundCharge,
      @RequestParam("serviceCharge") double serviceCharge) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return service.updateRefundOrderPrice(enterpriseId, username, id, airRefundCharge, serviceCharge);
  }

  /**
   * 修改退票单的备注
   */
  @PostMapping(value = "/{id}/remark")
  public JsonResult updateRefundOrderRemark(HttpServletRequest request,
      @PathVariable("id") int id,
      @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return service.updateRefundOrderRemark(enterpriseId, username, id, remark);
  }

  /**
   * 修改退票单的票号
   */
  @PostMapping(value = "/{id}/ticket")
  public JsonResult updateRefundOrderTicket(HttpServletRequest request,
      @PathVariable("id") int orderId,
      @RequestParam("ticketNo") String ticketNo
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return service.updateRefundOrderTicketNo(enterpriseId, username, orderId, ticketNo);
  }


  /**
   * CZ NDC 创建退票单
   */
  @PostMapping(value = "/{id}/externalOrderRefund")
  public JsonResult createNdcOrderRefund(HttpServletRequest request,
                                            @PathVariable("id") int orderId
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return service.createNdcOrderRefund(enterpriseId, username, orderId);
  }

  /**
   * CZ NDC 查询退票单
   */
  @PostMapping(value = "/{id}/externalOrderRefund/status")
  public JsonResult syncNdcOrderRefundInfo(HttpServletRequest request,
                                         @PathVariable("id") int orderId
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return service.syncNdcOrderRefundInfo(enterpriseId, username, orderId);
  }
}
